﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Entity;

namespace GoodMan.Infrastructure.Entities.Cache
{
    /// <summary>
    /// 参数只包含一个，为OrderId
    /// </summary>
    public class OrderCache : EntitiesCache<Order>
    {
        /// <summary>
        /// key 为 orderId
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public override Order GetItem(params string[] keys)
        {
            return base.GetItem(keys);
        }
        protected override bool ValidateParameter(params string[] keys)
        {
            return keys.Length == 1 && !String.IsNullOrEmpty(keys[0]);
        }
        protected override Order LoadDataItem(params string[] keys)
        {
            if (ValidateParameter(keys))
            {
                int id = Int32.Parse(keys[0]);
                var order = new Order().Find(c => c.Id == id);
                return order;
            }
            return null;
        }
        protected override string BuildKey(params string[] keys)
        {
            if (ValidateParameter(keys))
            {
                return String.Format("GetOrderEntity_{0}", keys[0]);
            }
            return string.Empty;
        }
        protected override string BuildKey(Order data)
        {
            if (data != null && data.Id != 0)
            {
                return BuildKey(data.UserName);
            }
            return string.Empty;
        }
    }
}
